package com.lihui.exercises;

import com.lihui.dao.ClassesDao;
import com.lihui.dao.StudentDao;
import com.lihui.model.Classes;
import com.lihui.model.Student;

import java.text.MessageFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * 功能描述
 *
 * @author wileda
 * @date 2022/12/28  17:05
 */
public class Exe_01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //用户输入班级名称
        System.out.println("请输入班级名称：");
        String classesName = scanner.nextLine();
         //根据班级名称去数据库查询班级编号
        ClassesDao classesDao = new ClassesDao();
        Classes classes = classesDao.selectByName(classesName);
        if(classes == null){
            System.out.println("你要找的班级不存在  " + classesName);
            return;
        }
        String classesInfo = MessageFormat.format("班级编号: {0}\t班级名称:{1}", classes.getId(), classes.getName());
        System.out.println(classesInfo);



        //录入学生信息
        System.out.println("============录入学生信息============");
        System.out.println("请输入学号: ");
        String sn = scanner.next();
        System.out.println("请输入姓名: ");
        String name = scanner.next();
        System.out.println("请输入邮箱: ");
        String mail = scanner.next();
        System.out.println("请输入班级编号: ");
        int classesId = scanner.nextInt();
        Student student = new Student();
        student.setSn(sn);
        student.setName(name);
        student.setMail(mail);
        student.setClassesId(classesId);

        StudentDao studentDao = new StudentDao();
        int result = studentDao.insert(student);
        if(result > 0){
            System.out.println("新增学生成功" + student.getName());
        }else {
            System.out.println("新增学生失败" + student.getName());
        }
    }
}
